package com.pingan.cashier.service.impl;


import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.pingan.cashier.common.util.WXPayUtil;
import com.pingan.cashier.dal.entity.PayOrder;
import com.pingan.cashier.dal.mapper.PayOrderMapper;
import com.pingan.cashier.web.req.WechatNotifyReq;
import com.pingan.cashier.web.resp.CommonResp;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;

/**
 * 微信回调通知
 */
@Service
public class WexinNotifyProcess {

    private static final Logger logger = LoggerFactory.getLogger(WexinNotifyProcess.class);

    @Resource
    private PayOrderMapper payOrderMapper;


    /**
     * 支付回调
     *
     * @param wechatNotifyStr
     * @return
     */
    public CommonResp weixinPayNotify(String wechatNotifyStr) {
        //判断返回报文里面的return_code，如果不是SUCCESS说明微信内部出现问题，直接报警
        // Map<String, String> notifyParam = WXPayUtil.xmlToMap(wechatNotifyStr);
        WechatNotifyReq req = JSON.parseObject(wechatNotifyStr, WechatNotifyReq.class);
        PayOrder payOrder = payOrderMapper.getByTradeNo(req.getId(), 1);
        if (payOrder == null) {
            return CommonResp.fail();
        }

        //支付成功,其他都认为失败
        payOrder.setResult("TRANSACTION.SUCCESS".equals(req.getEventType()) ? 1 : 2);
        payOrder.setUpdateTime(new Date());
        payOrder.setUserid("notify");
        payOrderMapper.updateOrder(payOrder);
        return CommonResp.success();


    }


}
